package org.jingouzhui.domain.strategy.service.rule.filter.impl;

import lombok.extern.slf4j.Slf4j;
import org.jingouzhui.domain.strategy.model.entity.RuleActionEntity;
import org.jingouzhui.domain.strategy.model.entity.RuleMatterEntity;
import org.jingouzhui.domain.strategy.repository.IStrategyRepository;
import org.jingouzhui.domain.strategy.service.annotation.LogicStrategy;
import org.jingouzhui.domain.strategy.service.rule.ILogicFilter;
import org.jingouzhui.domain.strategy.service.rule.filter.factory.DefaultLogicFactory;
import org.jingouzhui.domain.strategy.model.valboj.RuleLogicCheckTypeVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @description: 【抽奖中规则过滤】抽奖n次后才能解锁的奖品
 * @author: jingouzhui
 * @date: 2025/4/28 15:15
 */
@Slf4j
@Service
@LogicStrategy(logicMode = DefaultLogicFactory.LogicModel.RULE_LOCK)
public class RuleLockLogicFilter implements ILogicFilter<RuleActionEntity.RaffleCenterEntity> {


    @Resource
    private IStrategyRepository strategyRepository;
    /**
     * 用户抽奖次数，先固定，后续走mysql/redis
     */
    private final Integer userRaffleCount = 0;
    @Override
    public RuleActionEntity<RuleActionEntity.RaffleCenterEntity> filter(RuleMatterEntity ruleMatterEntity) {
        Long strategyId = ruleMatterEntity.getStrategyId();
        String userId = ruleMatterEntity.getUserId();
        String ruleModel = ruleMatterEntity.getRuleModel();
        Integer awardId = ruleMatterEntity.getAwardId();
        log.info("【抽奖中规则过滤】，抽奖n次后才能解锁，strategyId:{},userId:{},ruleModel:{}",strategyId,userId,ruleModel);
        String ruleValue = strategyRepository.queryStrategyRuleValue(strategyId, awardId, ruleModel);
        // 可以解锁不需要被接管
        if (userRaffleCount >= Integer.parseInt(ruleValue)) {
          return RuleActionEntity.<RuleActionEntity.RaffleCenterEntity>builder()
                  .code(RuleLogicCheckTypeVO.ALLOW.getCode())
                  .info(RuleLogicCheckTypeVO.ALLOW.getInfo())
                  .build();
        }
        // 用户抽奖次数小于规则限定值，规则拦截
        return RuleActionEntity.<RuleActionEntity.RaffleCenterEntity>builder()
                .code(RuleLogicCheckTypeVO.TAKE_OVER.getCode())
                .info(RuleLogicCheckTypeVO.TAKE_OVER.getInfo())
                .build();
    }
}
